import routerConfig from '../../page/router';
import routerInit from 'miniapp-router';
const dataSource = [

    {label:'option1', value:'option1'},

    {label:'option2', value:'option2'},

    {label:'disabled', disabled:true}

];
Component({

  mixins: [],
  data: {
    dataSource,
    loading: false,
    listData:[],
    videoData:[],
    material:[],
    styleName:'', //风格名称
    styleUrl:'',  //风格图片
    showId: 11, //动画id
    showUrl:'', //动画图片
    bjId: 1,
    syId: 4,
    name:'',
    modalVd: false,
    vdUrl:'', //预览视频
    singleData:{
      video_cover:'',
      video:'',
      sales_apply_id:'',
      sales_id:'',
    },
    pathText:'',
    pathPic:'',
    textShow:true,
    picShow:false,
    upImgUrl:'../image/pic.jpg',
    watermark:'LOGO.png',
    MergeList:[{"path": "input1.mp4"}],//参数视频地址
    WaterMark:{"text": {"Content": ""},
              "image": {"InputFile": ""}}, //参数水印
    bgAmix:'https://jcb001.oss-cn-shanghai.aliyuncs.com/dj123.mp3',//参数背景音乐
    bgVideo:'bgVideo/bg1.mp4', //参数背景风格
  },
  props: {},
  didMount() { 
    routerInit.call(this, routerConfig);  //跳转页面初始化
    this.getLists();
    this.getMaterial();
    // console.log(222222222222222);
    // console.log(this.data.singleData);
  },
  didUpdate() { },
  didUnmount() { },
  methods: {
    setSave(){
      this.setData({
        'WaterMark.text.Content':'杨坤',
        'WaterMark.image.InputFile':'yangkun.png'
      })
      console.log(this.data.WaterMark);
    },
    //合成视频
    dealWithVideo(){
      var that = this;
      if (!this.data.name) {
        my.alert({ content: '请给保存的视频定义名称!' });
        return false;
      }
      my.confirm({
        title: '确认',
        content: '确定保存该视频吗?',
        success: (result) => {
          if (result.confirm) {
            console.log("视频保存")
            var shuiYing;
            if(that.data.pathText && that.data.pathPic){
              shuiYing = {
                "text": {"Content": that.data.pathText},
                "image": {"InputFile": that.data.pathPic}
              }
            }else if(that.data.pathText){
              shuiYing = {
                "text": {"Content": that.data.pathText}
              }
            }else if(that.data.pathPic){
              shuiYing = {
                "image": {"InputFile": that.data.pathPic}
              }
            }else{
              shuiYing = {}
            }
            var apiurl = 'App.ShopRedsVideoCore.DealWithVideo';
            var params = {
              sales_apply_id:that.data.singleData.sales_apply_id,
              sales_id:that.data.singleData.sales_id,
              name:that.data.name,
              bgAmix: that.data.bgAmix,
              bgVideo: that.data.styleUrl,
              WaterMark: JSON.stringify(shuiYing),
              watermark:that.data.watermark,
              MergeList: JSON.stringify(that.data.MergeList)
            };
             my.alert({ title: params});
            console.log(params)
            var app = getApp();
            app.xdapi(apiurl, params, function (res) {
              my.hideLoading();
            });
            setTimeout(() => {
               my.alert({
                title: '已经加入合成任务队列',
                content: '可在“合成任务”页进行查看',
                buttonText: '我知道了',
                success: () => {
                  let $router = that.$page.$router;
                  $router.push(`/api/video-task`);
                }
              });
            }, 2000);
          }
        }
      });
    },
    /** 上传图片 */
    upImg() {
      var that = this;
      my.qn.chooseFile({
        count: 2,   // 返回的文件数，默认 1
        type: 0x01, // 图片=0x01，文档=0x02，多媒体=0x04，自由组合，默认 0x01
        success: (res) => {
          my.showLoading({ content: '上传中...' });
          // my.alert({ title: res.apFilePaths[0] });
          const { cloud } = getApp();
          var result = cloud.file.uploadFile({
            filePath: res.apFilePaths[0],
            fileType: 'image',
          });
          result.then(function (data) {
            // 提交到后端处理
            my.hideLoading();
            that.picOss(data.url); 
          });
        },
      });
    },
    //上传图片到oss
    picOss(url){
      // my.alert({ title: url });
      var app = getApp();
      var that = this;
      var apiurl = "App.ShopRedsVideoCore.UploadFile";
      var params = {'url':url};
      app.xdapis(apiurl, params, function (res) {
        my.hideLoading();
        console.log(res);
        // my.alert({ title: res });
        that.setData({
          upImgUrl:res.url,
          'WaterMark.image.InputFile': res.url,
          picShow: res.url,
        })
      });
    },
    //关闭视频模态框
    closeVideo(e) {
      var name = e.currentTarget.dataset.name;
      this.setData({
        [name]: false,
        vdUrl: '',
      });
    },
    //预览视频
    openVideo(e){
      var src = e.currentTarget.dataset.src;
      this.setData({
        vdUrl: src,
        modalVd: true,
      });
    },
    //保存视频
    saveVideo(){
      var that = this;
      my.confirm({
        title: '确认',
        content: '确定保存该视频吗?',
        success: (result) => {
          if (result.confirm) {
            console.log("视频保存")
            var apiurl = 'App.ShopRedsVideo.Save';
            var params = {
              ...that.data.singleData,
              name:that.data.name,
              style: that.data.styleUrl,
              animation: that.data.showUrl,
              music: that.data.showUrl,
              watermark: that.data.showUrl
            };
            console.log(params)
            var app = getApp();
            app.xdapis(apiurl, params, function (e) {
              my.hideLoading();
              my.alert({
                title: '保存成功！',
                content: '可以在视频列表进行投放',
                buttonText: '我知道了',
                success: () => {
                  // my.alert({
                  //   title: '用户点击了「我知道了」',
                  // });
                }
              });
             
            });
          }
        }
      });
    },
    //选中视频
    yesVideo(e){
      var that = this;
      // var video_cover = e.target.dataset.video_cover;
      // var video = e.target.dataset.video;
      console.log(e)
      that.setData({
        'singleData.duration':e.target.dataset.duration,
        'singleData.sales_apply_id':e.target.dataset.id,
        'singleData.video_cover': e.target.dataset.src,
        'singleData.video': e.target.dataset.vd,
      })
      console.log(that.data.singleData)
    },
    //删除视频
    delVideo(){
       var that = this;
      that.setData({
        name:'',
        'singleData.video_cover': '',
        'singleData.video': '',
      })
    },
    //定义名称
    setTitle(e){
      this.setData({
        name: e.detail.value,
      })
      console.log(this.data.name);
    },
    //定义水印名称
    setTextShui(e){
      this.setData({
        'WaterMark.text.Content': e.detail.value,
        pathText: e.detail.value
      })
      console.log(this.data.name);
    },
    //选中活动
    selectAct(e){
      console.log(e)
      this.setData({
        'singleData.sales_id':e.detail.value,
        name:'',
        'singleData.video_cover': '',
        'singleData.video': '',
      })
    },
    //去查询
    toAct(){
      this.getVideo(this.data.singleData.sales_id);
    },
    //获取活动
    getLists() {
      var app = getApp();
      var that = this;
      var apiurl = "App.ShopRedsVideo.GetPassSalesList";
      var params = { 'pagesize': 10, page: 1 };
      app.xdapis(apiurl, params, function (res) {
        my.hideLoading();
        console.log(res);
        that.setData({
          listData: res.list
        })
        if(res.list.length > 0){
          that.getVideo(res.list[0].sales_id)
          that.setData({
            'singleData.sales_id':res.list[0].sales_id,
          })
        }else{
          that.getVideo(1)
        }
        console.log(that.data.listData);
      });
    },
    //获取素材
    getMaterial() {
      var app = getApp();
      var that = this;
      var apiurl = "App.ShopRedsInfos.GetBaseVideoMaterial";
      var params = { 'pagesize': 10, page: 1 };
      app.xdapis(apiurl, params, function (res) {
        my.hideLoading();
        console.log(555555);
        console.log(res);
        // my.alert({ title: res});
        that.setData({
          material: res.list,
          showId: res.list.animation[0].materials_id,
          showUrl: res.list.animation[0].url,
          styleName: res.list.video_style[0].name,
          styleUrl: res.list.video_style[0].url,
        })
        console.log('clic111111k');
        console.log(that.data.material);
      });
    },
    //根据活动获取视频
    getVideo(sales_id){
      var app = getApp();
      var that = this;
      var apiurl = "App.ShopRedsVideo.GetAvailableList";
      var params = { 'pagesize': 20, page: 1 ,sales_id};
      app.xdapis(apiurl, params, function (res) {
        my.hideLoading();
        console.log(res);
        that.setData({
          videoData: res
        })
        console.log(that.data.videoData);
      });
    },
    //选择风格
    selectStyle(e) {
      var that = this;
      var name = e.target.dataset.name;
      var url = e.target.dataset.url;
      console.log(e)
      that.setData({ 
        styleName: name,
        styleUrl: url,
      })
      console.log(that.data.styleName)
      console.log(that.data.styleUrl)
    },
    ohmygod(e) {
      console.log('clic111111k');
      console.log(e);
    },
    dealTap: function (e) {
      var that = this;
      var name = e.target.dataset.name;
      var id = e.target.dataset.id;
      // console.log(id)
      // console.log(name)
      that.setData({
        [name]: id
      })
    },
    setText(e){
      var that = this;
      var name = e.target.dataset.name;
      var id = e.target.dataset.id;
      that.setData({
        [name]: id,
        textShow:true,
        picShow:false,
      })
    },
    setPic(e){
      var that = this;
      var name = e.target.dataset.name;
      var id = e.target.dataset.id;
      that.setData({
        [name]: id,
        textShow:false,
        picShow:true,
      })
    },
    setLoading() {
      this.setData({
        loading: !this.data.loading
      })
    }
  },
});
